package com.jiulan.hearthearing.ui.news.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jiulan.hearthearing.R;
import com.jiulan.hearthearing.base.BaseActivity;
import com.jiulan.hearthearing.constants.UrlConstants;
import com.jiulan.hearthearing.http.HttpRequest;
import com.jiulan.hearthearing.http.HttpStringCallBack;
import com.jiulan.hearthearing.ui.news.model.NewsDetailsModel;
import com.shehuan.niv.NiceImageView;
import com.zzhoujay.richtext.ImageHolder;
import com.zzhoujay.richtext.RichText;

import java.util.HashMap;

/**
 * 新闻详情
 */
public class NewsDetailsActivity extends BaseActivity {
    private int id;
    private NewsDetailsModel model;

    private TextView tvTitle;
    private NiceImageView nivPic;
    private TextView tvAuthor;
    private TextView tvTime;
    private TextView tvContent;
    private TextView tvViewNum;

    public static void actionStart(Context context, int id) {
        Intent intent = new Intent(context, NewsDetailsActivity.class);
        intent.putExtra("id", id);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_news_details;
    }

    @Override
    protected void initView() {
        id = getIntent().getIntExtra("id", 0);
        setTitleText("新闻详情");
        setRightImg(R.mipmap.zhuanfa_news);
        tvTitle = findViewById(R.id.tvTitle);
        nivPic = findViewById(R.id.nivPic);
        tvAuthor = findViewById(R.id.tvAuthor);
        tvTime = findViewById(R.id.tvTime);
        tvContent = findViewById(R.id.tvContent);
        tvViewNum = findViewById(R.id.tvViewNum);
        //在第一次调用RichText之前先调用RichText.initCacheDir()方法设置缓存目录，不设置会报错
        RichText.initCacheDir(this);
        getData();
    }

    private void getData() {
        new HttpRequest(getContext()).doGet(UrlConstants.news_details + String.valueOf(id), "", new HashMap(), NewsDetailsModel.class, new HttpStringCallBack() {
            @Override
            public void onSuccess(Object result) {
                if (result instanceof NewsDetailsModel) {
                    model = (NewsDetailsModel) result;
                    setView();
                }
            }

            @Override
            public void onFailure(int code, String msg) {

            }
        });
    }

    private void setView() {
        tvTitle.setText(model.getData().getTitle());
        Glide.with(getContext()).load(model.getData().getAuthor_pic()).into(nivPic);
        tvAuthor.setText(model.getData().getAuthor());
        tvTime.setText(model.getData().getAdd_time());
        tvViewNum.setText(String.valueOf(model.getData().getView()) + "人已阅读");
        RichText.from(model.getData().getContent()).bind(this)
                .showBorder(false)
                .size(ImageHolder.MATCH_PARENT, ImageHolder.WRAP_CONTENT)
                .into(tvContent);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //结束时清空内容
        RichText.clear(this);
    }

    /**
     * 转发分享
     *
     * @param v
     */
    @Override
    public void onTitleBarRightClick(View v) {
        super.onTitleBarRightClick(v);

    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getTitleBarType() {
        return TITLEBAR_RIGHT_IMG;
    }
}
